'use strict';

const dataConst = require('../constant/data-const');

const Service = require('egg').Service;

class VerifyService extends Service {
    async getUserVerifyStateSrv(userId) {
        const { app } = this;

        const result = await app.model.UserVerify.findAll({
            where: {
                user_id: userId
            }
        });

        const verifyTypeArray = Object.entries(dataConst.verifyType);
        const verifyInfo = {};
        result.forEach(item => {
            const { verify_type, verify_state } = item;
            for (const data of verifyTypeArray) {
                const [key, value] = data;
                if (verify_type === value.value) {
                    verifyInfo[key] = verify_state;
                    break;
                }
            }
        });
        return verifyInfo;
    }

    async setUserVerifyStateSrv(userId, verifyType, verifyState, content) {
        const { app } = this;

        const result = await app.model.UserVerify.findOne({
            where: {
                user_id: userId,
                verify_type: verifyType
            }
        });

        if (result) {
            result.verify_state = verifyState;
            result.content = content;
            return await result.save();
        } else {
            return await app.model.UserVerify.create({
                user_id: userId,
                verify_type: verifyType,
                verify_state: verifyState,
                content
            });
        }
    }

    async getVerifyApplyListSrv({verifyType, verifyState, limit, offset}) {
        const { ctx } = this;

        let queryString =
            `select
                uv.*, usr.id, usr.username, usr.nickname, usr.gender
                , concat(:serverUrl, :avatarPath, usr.id, '/', usr.avatar) as avatar_url
                from tbl_user usr
                left join tbl_user_verify uv
                on usr.id = uv.user_id
                where true`;

        const paramsData = {
            serverUrl: dataConst.serverUrl,
            avatarPath: dataConst.avatarPath
        };

        if (verifyType && parseInt(verifyType)) {
            queryString += ` and uv.verify_type = :verifyType`;
            paramsData.verifyType = verifyType;
        }

        if (verifyState && parseInt(verifyState)) {
            queryString += ` and uv.verify_state = :verifyState`;
            paramsData.verifyState = verifyState;
        }

        queryString += ` order by uv.updated_at desc`;

        return await ctx.helper.sqlQueryCount(queryString, paramsData, limit, offset);
    }

    async createMemberComplaintSrv(userId, targetUserId, content, image) {
        const { app } = this;

        return await app.model.MemberComplaint.create({
            user_id: userId,
            target_user_id: targetUserId,
            type: dataConst.verifyStateType.apply.value,
            content,
            image
        });
    }

    async verifyMemberComplaintSrv(complaintId, verifyState, replay) {
        const { app } = this;

        return await app.model.MemberComplaint.update({
            type: verifyState,
            replay
        }, {
            where: {
                id: complaintId
            }
        });
    }

    async getMemberComplaintStateSrv(userId) {
        const { app } = this;

        const count = await app.model.MemberComplaint.count({
            where: {
                target_user_id: userId,
                type: dataConst.verifyStateType.certified.value,
                state: dataConst.stateType.active
            }
        });

        return count > 0;
    }

    async getMemberComplaintListSrv(queryData, limit, offset) {
        const { ctx } = this;
        const { userId, targetUserId, verifyState } = queryData;

        let queryString =
            `select
                mc.*, usr.username, usr.nickname, usr.gender
                from tbl_member_complaint mc
                left join tbl_user usr
                on mc.target_user_id = usr.id
                where true
                and mc.state = :state`;

        const paramsData = {
            state: dataConst.stateType.active
        };

        if (userId) {
            queryString += ` and mc.user_id = :userId`;
            paramsData.userId = userId;
        }
        if (targetUserId) {
            queryString += ` and mc.target_user_id = :targetUserId`;
            paramsData.targetUserId = targetUserId;
        }
        if (verifyState && parseInt(verifyState)) {
            queryString += ` and mc.type = :verifyState`;
            paramsData.verifyState = verifyState;
        }

        queryString += ` order by mc.updated_at desc`;

        return await ctx.helper.sqlQueryCount(queryString, paramsData, limit, offset);
    }

    async getUserVerifyResultSrv(userId) {
        const { app } = this;

        const verify = await app.model.UserVerify.findAll({
            where: {
                user_id: userId
            }
        });

        const complaint = await app.model.MemberComplaint.findAll({
            where: {
                target_user_id: userId
            }
        });

        return {
            verify,
            complaint
        }
    }
}

module.exports = VerifyService;
